/******************************************************************************* * Copyright (c) 2012, Project: FP7-ICT-257930 Aniketos * All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * - Neither the name of institution nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************/ package org.activiti.designer.eclipse.common; import java.awt.Dimension; import java.awt.Toolkit; import org.activiti.designer.elsag.Activator; import org.apache.log4j.Logger; import org.eclipse.jface.action.ICoolBarManager; import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.swt.SWT; import org.eclipse.swt.events.MenuDetectEvent; import org.eclipse.swt.events.MenuDetectListener; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.events.ShellAdapter; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.widgets.Menu; import org.eclipse.swt.widgets.MenuItem; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Tray; import org.eclipse.swt.widgets.TrayItem; import org.eclipse.ui.IWorkbenchWindow; import org.eclipse.ui.application.ActionBarAdvisor; import org.eclipse.ui.application.IActionBarConfigurer; import org.eclipse.ui.application.IWorkbenchWindowConfigurer; import org.eclipse.ui.application.WorkbenchWindowAdvisor; /** * @author Francesco Malmignati, Selex ES S.p.A. * */ public class ApplicationWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor { private IWorkbenchWindow window; private TrayItem trayItem; private Image trayImage; final private static Logger log = Logger.getLogger(ApplicationWorkbenchWindowAdvisor.class); public ApplicationWorkbenchWindowAdvisor(IWorkbenchWindowConfigurer configurer) { super(configurer); } public ActionBarAdvisor createActionBarAdvisor(IActionBarConfigurer configurer) { return new ApplicationActionBarAdvisor(configurer); } public void preWindowOpen() { Toolkit toolkit = Toolkit.getDefaultToolkit (); Dimension dim = toolkit.getScreenSize(); IWorkbenchWindowConfigurer configurer = getWindowConfigurer(); configurer.setInitialSize(new Point(dim.width, dim.height)); configurer.setShowCoolBar(true); configurer.setShowStatusLine(false); configurer.setTitle("Service Composition Framework"); //$NON-NLS-1$ } public void postWindowOpen() { super.postWindowOpen(); IActionBarConfigurer configurer = getWindowConfigurer() .getActionBarConfigurer(); ICoolBarManager coolBarManager = configurer.getCoolBarManager(); coolBarManager.setLockLayout(true); window = getWindowConfigurer().getWindow(); trayItem = initTaskItem(window); // Some OS might not support tray items if (trayItem != null) { minimizeBehavior(); // Create exit and about action on the icon hookPopupMenu(); } } public boolean preWindowShellClose(){ boolean response = askQuestion(); if(response){ log.debug("Closing the SCF"); window.getWorkbench().close(); return true; } else { log.debug("Putting the SCF as not visible"); getWindowConfigurer().getWindow().getShell().setVisible(false); return false; } } private void minimizeBehavior() { window.getShell().addShellListener(new ShellAdapter() { // If the window is minimized hide the window // public void shellIconified(ShellEvent e) { // window.getShell().setVisible(false); // } // public void shellClosed(ShellEvent e) { // window.getShell().setVisible(false); // } }); // If user double-clicks on the tray icons the application will be // visible again trayItem.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { Shell shell = window.getShell(); if (!shell.isVisible()) { window.getShell().setMinimized(false); shell.setVisible(true); } } }); } // We hook up on menu entry which allows to close the application private void hookPopupMenu() { trayItem.addMenuDetectListener(new MenuDetectListener() { @Override public void menuDetected(MenuDetectEvent e) { Menu menu = new Menu(window.getShell(), SWT.POP_UP); // Creates a new menu item that terminates the program MenuItem exit = new MenuItem(menu, SWT.NONE); exit.setText("Exit"); exit.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { window.getWorkbench().close(); } }); // We need to make the menu visible menu.setVisible(true); } }); } // This methods create the tray item and return a reference private TrayItem initTaskItem(IWorkbenchWindow window) { final Tray tray = window.getShell().getDisplay().getSystemTray(); TrayItem trayItem = new TrayItem(tray, SWT.NONE); trayImage = Activator.getImageDescriptor("/icons/aniketos.png") .createImage(); trayItem.setImage(trayImage); trayItem.setToolTipText("Service Composition Framework"); return trayItem; } // We need to clean-up after ourself @Override public void dispose() { if (trayImage != null) { trayImage.dispose(); } if (trayItem != null) { trayItem.dispose(); } } private boolean askQuestion() { boolean response = MessageDialog.openQuestion(window.getShell(), "Closing", "Are you sure you want to close the Service Composition Framework?\n\n(Click No to minimize it as tray icon)"); return response; } }